package br.com.linkers.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.linkers.data.HibernateUtil;

import br.com.linkers.entity.Amizades;

public class AmizadeDAO {
	Session session = HibernateUtil.getSessionFactory().openSession();
	Transaction transaction = session.beginTransaction();

	List<Amizades> lista;
	
	public void newSession(){
		this.session = HibernateUtil.getSessionFactory().openSession();
		this.transaction = session.beginTransaction();
	}
	
	public void inserir(Amizades amizade) {
		newSession();
		
		session.persist(amizade);
		transaction.commit();
		session.close();
	}

	public void deletar(Amizades amizade) {
		newSession();
		
		session.delete(amizade);
		transaction.commit();
		session.close();
	}
	
	public Amizades buscarPorId(Long id) {
		return (Amizades) session.load(Amizades.class, id);
	}
	
	public List<Amizades> listar() {
		newSession();
		Criteria cri = session.createCriteria(Amizades.class);
		this.lista = cri.list();
		return this.lista;
	}
}
